package com.toman.toolbar;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.ColorInt;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

import com.toman.MyApplication;
import com.toman.R;
import com.toman.Util.PermissonUtils;
import com.toman.Util.SPUtils;
import com.example.swipebackactivity.SwipeBackLayout;
import com.example.swipebackactivity.SystemBarTintManager;
import com.example.swipebackactivity.Utils;
import com.example.swipebackactivity.app.SwipeBackActivity;
import com.example.swipebackactivity.app.SwipeBackActivityBase;
import com.example.swipebackactivity.app.SwipeBackActivityHelper;
import com.githang.statusbar.StatusBarCompat;
import com.kingja.loadsir.core.LoadService;

import butterknife.ButterKnife;


/**
 * @Description:
 * @Author:邹苏隆
 * @Since:2016/12/16 10:54
 */
public abstract class ToolBarWhiteActivity extends AppCompatActivity  implements SwipeBackActivityBase, SwipeBackLayout.FinishActivityListener  {
   public LoadService loadService;
    private ToolBarHelper mToolBarHelper;
    public Toolbar toolbar;
    private TextView tvTitle;
    public Handler mHandler;
    public int mLayoutResID;
    public MyApplication myApplication;
    public String token;
    private SwipeBackActivityHelper mHelper;
    @ColorInt
    private int mStatusBarColor;

    public String msg;
    protected boolean Landscape = false;
public String id;
    public   String companyId;
    public   String mCompanyId;

    public Animation slide_left_to_left,slide_right_to_left,slide_left_to_right,slide_left_to_left_in;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.colorPrimary), false);
    }

    @Override
    public void setContentView(int layoutResID) {
        mHelper = new SwipeBackActivityHelper(this);
        mHelper.onActivityCreate();
        mToolBarHelper = new ToolBarHelper(this, layoutResID);
//                mToolBarHelper.setToolbarTitle("a");
        toolbar = mToolBarHelper.getToolBar();

        setContentView(mToolBarHelper.getContentView());
        ButterKnife.bind(this);

        /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolbar);
        /*自定义的一些操作*/
        onCreateCustomToolBar(toolbar);
        mLayoutResID = layoutResID;
        myApplication = (MyApplication) getApplication();
        token = (String) SPUtils.get(getApplicationContext(),"token","");
         slide_left_to_left = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_left_to_left);
         slide_right_to_left = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_right_to_left);
         slide_left_to_right = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_left_to_right);
         slide_left_to_left_in = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.slide_left_to_left_in);
//        mToolBarHelper.setToolbarTitle("a");
        if (SPUtils.contains(getApplicationContext(),"cpy_id_now")){
            companyId= (String) SPUtils.get(getApplicationContext(),"cpy_id_now","");
        }
        else
        {
                companyId= (String) SPUtils.get(getApplicationContext(),"cpy_id","");

        }
        mCompanyId=(String) SPUtils.get(getApplicationContext(),"cpy_id","");
        getWindow().setSoftInputMode( WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

    }

    public void onCreateCustomToolBar(Toolbar toolbar) {
        tvTitle = (TextView) findViewById(R.id.tv_toolbar_title);
        toolbar.setContentInsetsRelative(0, 0);
        toolbar.inflateMenu(R.menu.toolbar_save);

    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
//        setStateBarColor(this);
        mHelper.onPostCreate();
        getSwipeBackLayout().setOnFinishActivityListener(this);
    }

    @Override
    protected void onResume() {
        /**
         * 设置为横屏
         */
//        if (Landscape) {
//            if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
//                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//            }
//        } else {
//        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
//            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
//        }
//        }
        super.onResume();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();

            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    public void onPause() {
        super.onPause();
    }



    public void initToolBar(String text, @ColorInt int colorId) {
        mToolBarHelper.setTvTitle(text);
        mToolBarHelper.setToolBarColor(colorId);
        StatusBarCompat.setStatusBarColor(this, colorId, false);


        super.setTitle("");
        toolbar = mToolBarHelper.getToolBar();
        if (colorId==getResources().getColor(R.color.white)){
            setArrowBlack();
            mStatusBarColor=colorId;
            mToolBarHelper.setTvColor(getResources().getColor(R.color.black));

            StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.white), true);

        }
        else {
        }
  /*把 toolbar 设置到Activity 中*/
        setSupportActionBar(toolbar);

    }
    public void initToolBar(String text) {
initToolBar(text,getResources().getColor(R.color.white));


    }


    public void isLandscape() {
        Landscape = true;
    }

    public void setTvTitle(String text) {
        mToolBarHelper.setTvTitle(text);
    }
    public void setArrowBlack(){
        toolbar.setNavigationIcon(R.mipmap.back_black);
    }
    @Override
    public SwipeBackLayout getSwipeBackLayout() {
        return mHelper.getSwipeBackLayout();
    }

    @Override
    public void setSwipeBackEnable(boolean enable) {
        getSwipeBackLayout().setEnableGesture(enable);
    }

    @Override
    public void scrollToFinishActivity() {
        Utils.convertActivityToTranslucent(this);
        getSwipeBackLayout().scrollToFinishActivity();
    }

    @Override
    public void dothingBeforeFinish() {

    }
    public void setStateBarColor(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            //5.0 全透明实现
            //getWindow.setStatusBarColor(Color.TRANSPARENT)
            Window window = activity.getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //4.4 全透明状态栏
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        // 设置状态栏颜色
        ViewGroup contentLayout = (ViewGroup) activity.getWindow().getDecorView().findViewById(android.R.id.content);

        SystemBarTintManager systemBarTintManager = new SystemBarTintManager(activity);
        SystemBarTintManager.SystemBarConfig config = systemBarTintManager.getConfig();
        int actionBarHeight = config.getActionBarHeight();
        contentLayout.getChildAt(0).setPadding(0, getStatusBarHeight(activity) + actionBarHeight, 0, 0);
//        if (mStatusBarColor == 0) {
//            setupStatusBarView(activity, contentLayout, Color.parseColor("#cccccc"));
//        }else {
//            setupStatusBarView(activity, contentLayout, mStatusBarColor);
//        }
        // 设置Activity layout的fitsSystemWindows
        View contentChild = contentLayout.getChildAt(0);
        contentChild.setFitsSystemWindows(true);//等同于在根布局设置android:fitsSystemWindows="true"
    }
    private static void setupStatusBarView(Activity activity, ViewGroup contentLayout, int color) {
        View mStatusBarView = null;
        View statusBarView = new View(activity);
        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, getStatusBarHeight(activity));
        contentLayout.addView(statusBarView, lp);
        mStatusBarView = statusBarView;
        mStatusBarView.setBackgroundColor(color);

    }

    /**
     * 获得状态栏高度
     */
    private static int getStatusBarHeight(Context context) {
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        return context.getResources().getDimensionPixelSize(resourceId);
    }
}